#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

int main()
{
    pid_t id = fork();
    if (0 == id)
    {
        //让子进程被替换
        execl("/usr/bin/ls", "ls", "-l", NULL);//可变参数列表;    语法规定, 必须以NULL结尾   
    }
    else 
    {
        int status = 0;
        pid_t rid = waitpid(id, &status, 0);
        if (rid > 0)
        {
            //一直等待目标进程
            if (WIFEXITED(status))
            {
                //正常结束
                printf("child pid:%d, exit_code:%d\n", id, WEXITSTATUS(status));
            }
            else
            {
                //出现异常
                printf("child pid:%d, exit_signal:%d\n", id, status & 0x7f);
            }
        }
        else // -1 == rid
        {
            
        }
    }

    return 0;
}
